﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

using Kooboo.Globalization;
using Kooboo.Commerce;
using Kooboo.Commerce.Contexts.Brands;
using Kooboo.Commerce.Contexts.Categories;

namespace Kooboo.Web.Areas.Commerce.Web.UI
{
    public static class Menu
    {

        static Menu()
        {

        }


        public static MvcHtmlString MenuFor(this HtmlHelper html)
        {
            string url = string.Empty;
            if (html.ViewContext.RequestContext.RouteData.DataTokens.ContainsKey("menu"))
            {
                url = html.ViewContext.RequestContext.RouteData.DataTokens["menu"].ToString();
            }
            else
            {
                url = html.ViewContext.HttpContext.Request.Url.ToString();
            }

            var items = new List<MenuItem>
            {
                new MenuItem{ Name= "/commerce/products", Text="Products".Localize(), ClassName="Product", Url = "/commerce/products", SubMenus=new List<MenuItem>()
                {
                      new BrandMenuItem{ Name= "/commerce/products/brands", Text="Brands".Localize(), ClassName="Brand", Url = "/commerce/products/brands"}, 
                      new CategoryMenuItem{ Name= "/commerce/products/categories", Text="Categories".Localize(), ClassName="Category", Url = "/commerce/products/categories"}
                }},                
              
                new MenuItem{ Name= "/commerce/orders", Text="Orders".Localize(), ClassName="Order", Url = "/commerce/orders",SubMenus = new List<MenuItem>()}, 
                new MenuItem{ Name= "/commerce/shop/", Text="Shop".Localize(), ClassName="Shop", Url = "/commerce/shop/promotions", SubMenus=new List<MenuItem>
                {
                    new MenuItem{ Name= "/commerce/shop/promotions", Text="Promotions".Localize(), ClassName="PromotionRule", Url = "/commerce/shop/promotions", SubMenus=new List<MenuItem>
                    {
                        new MenuItem{ Name= "/commerce/shop/promotions/extensions", Text="Extensions".Localize(), ClassName="Extensions", Url = "/commerce/shop/promotions/extensions", SubMenus=new List<MenuItem>{}}
                    }},
                    new MenuItem{ Name= "/commerce/shop/payments", Text="Payments".Localize(), ClassName="Payment", Url = "/commerce/shop/payments", SubMenus=new List<MenuItem>{}},
                    new MenuItem{ Name= "/commerce/shop/shippings", Text="Shippings".Localize(), ClassName="Shipping", Url = "/commerce/shop/shippings", SubMenus=new List<MenuItem>{}},
                    new MenuItem{ Name= "/commerce/shop/taxes", Text="Taxes".Localize(), ClassName="Tax", Url = "/commerce/shop/taxes", SubMenus=new List<MenuItem>{}}
                }},   
                new MenuItem{ Name= "/commerce/customers", Text="Customers".Localize(), ClassName="Customers", Url = "/commerce/customers", SubMenus=new List<MenuItem>{}},
                new MenuItem{ Name= "/commerce/services", Text="Service".Localize(), ClassName="Service", Url = "/commerce/services/supports", SubMenus=new List<MenuItem>
                {
                    new MenuItem{ Name= "/commerce/services/supports", Text="Support tickets".Localize(), ClassName="SupportTicket", Url = "/commerce/services/supports", SubMenus=new List<MenuItem>{}},
                    new MenuItem{ Name= "/commerce/services/questions", Text="Questions".Localize(), ClassName="ProductQuestion", Url = "/commerce/services/questions", SubMenus=new List<MenuItem>{}},
                    new MenuItem{ Name= "/commerce/services/reviews", Text="Reviews".Localize(), ClassName="ProductReview", Url = "/commerce/services/reviews", SubMenus=new List<MenuItem>{}}
                }},   
                new MenuItem{ Name= "/commerce/settings", Text="System".Localize(), ClassName="System", Url = "/commerce/settings/index", SubMenus=new List<MenuItem>
                {
                    new MenuItem{ Name= "/commerce/settings/index", Text="Settings".Localize(), ClassName="Settings", Url = "/commerce/settings/index", SubMenus=new List<MenuItem>()},
                    new MenuItem{ Name= "/commerce/settings/producttypes", Text="Product types".Localize(), ClassName="producttypes", Url = "/commerce/settings/producttypes", SubMenus=new List<MenuItem>{}}                    
                }},
                /*
                new MenuItem{ Name= "/commerce/reports", Text="Reports".Localize(), ClassName="Report", Url = "/commerce/reports/order", SubMenus=new List<MenuItem>
                {
                    new MenuItem{ Name= "/commerce/reports/order", Text="Order".Localize(), ClassName="report-order", Url = "/commerce/reports/order", SubMenus=new List<MenuItem>()},
                    new MenuItem{ Name= "/commerce/reports/customer", Text="Customer".Localize(), ClassName="report-customer", Url = "/commerce/reports/customer", SubMenus=new List<MenuItem>{}}                    
                }}*/
            };

            var menu = "<ul>";

            foreach (var i in items)
            {
                menu += BuildItem(i, items, url);
            }

            menu += "</ul>";
            return MvcHtmlString.Create(menu);
        }

        static string BuildItem(MenuItem item, List<MenuItem> items, string url)
        {
            var selected = item.HasSelected(url);
            string html = "<li class='" + (selected ? "current " : "") + (item.ClassName ?? "") + "' ><a href='" + item.Url + "' >" + System.Web.HttpUtility.HtmlEncode(item.Text) + "</a>";

            if (selected && item.SubMenus.Count > 0)
            {
                html += "<ul>";
                foreach (var i in item.SubMenus)
                {
                    html += BuildItem(i, items, url);
                }

                html += "</ul>";
            }

            html += "</li>";

            return html;
        }



        public class MenuItem
        {
            public virtual List<MenuItem> SubMenus
            {
                get;
                set;
            }

            public virtual string Name
            {
                get;
                set;
            }

            public string Text
            {
                get;
                set;
            }

            public virtual string Url
            {
                get;
                set;
            }

            public string ClassName
            {
                get;
                set;
            }

            public virtual bool HasSelected(string url)
            {
                return url.IndexOf(this.Name, StringComparison.OrdinalIgnoreCase) > -1;
            }
        }

        public class CategoryMenuItem : MenuItem
        {
            ServiceHost Host = new ServiceHost();

            public ICategory Category
            {
                get;
                set;
            }



            public override string Url
            {
                get
                {
                    if (base.Url == null)
                    {

                        if (this.Name.EndsWith("-"))
                        {
                            base.Url = this.Name.Substring(0, this.Name.Length - 1) + "/products";
                        }
                        else
                        {
                            base.Url = this.Name + "/products";
                        }
                    }
                    return base.Url;
                }
                set
                {
                    base.Url = value;
                }
            }

            public override bool HasSelected(string url)
            {
                if (this.Category == null)
                {
                    return base.HasSelected(url);
                }
                else
                {
                    return url.IndexOf("commerce/products/categories") > -1 && url.IndexOf(this.Category.Id.ToString()) > -1;
                }
            }

            List<MenuItem> _SubMenus;
            public override List<MenuItem> SubMenus
            {
                get
                {
                    if (this._SubMenus == null)
                    {
                        var query = this.Category == null ? this.Host.QueryCategories().Where(i => i.Parent == null) : Category.Children;

                        var list = query.OrderBy(i => i.Name)
                                .Select(i => new CategoryMenuItem
                                {
                                    Category = i,
                                    Name = (this.Name.EndsWith("/") || this.Name.EndsWith("-")) ? (this.Name + i.Id.ToString() + "-") : (this.Name + "/" + i.Id.ToString() + "-"),
                                    Text = i.Name + "(" + i.Products.Count() + ")",
                                    ClassName = "Product",

                                });


                        this._SubMenus = list.OfType<MenuItem>().ToList();


                    }

                    return this._SubMenus;
                }
                set
                {
                    this._SubMenus = value;
                }
            }


        }

        public class BrandMenuItem : MenuItem
        {
            ServiceHost Host = new ServiceHost();

            List<MenuItem> _SubMenus;
            public override List<MenuItem> SubMenus
            {
                get
                {
                    if (this._SubMenus == null)
                    {
                        this._SubMenus = this.Host
                            .QueryBrands()
                            .OrderBy(i => i.Name)
                            .Select(i => new MenuItem                 
                            {                        
                                Name = "/commerce/products/brands/" + i.Id.ToString() + "/products",                        
                                Text = i.Name + "(" + i.Products.Count().ToString() + ")",                        
                                ClassName = "",                        
                                Url = "/commerce/products/brands/" + i.Id.ToString() + "/products",                        
                                SubMenus = new List<MenuItem>()                    
                            }).ToList();
                    
                    }

                    return this._SubMenus;
                }
                set
                {
                    this._SubMenus = value;
                }
            }

        }

    }
}
